﻿using CommonUtil.Common;
using ProxyCrawler.Entity;
using ProxyCrawler.ProxyProviders;
using Autofac;
using System;
using System.Collections.Generic;
using Topshelf;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProxyCrawler.Job;
using CommonUtil.Common.Helpers;
using CommonUtil.Common.Log;

namespace ProxyCrawler
{
    public class ProxyService
    {
        public static void Start()
        {
            Init();
            new SyncProxyJob().Execute(null);

            //try
            //{
            //    HostFactory.Run(host =>
            //    {
            //        host.RunAsLocalSystem();
            //        host.StartAutomatically();

            //        host.SetServiceName("ProxyCrawler");
            //        host.SetDisplayName("ProxyCrawler");
            //        host.SetDescription("代理爬虫");

            //        //host.DependsOn("Redis");

            //        host.Service<QuartzService>(service =>
            //        {
            //            service.ConstructUsing(() => new QuartzService());
            //            service.WhenStarted(x => x.Start());
            //            service.WhenStopped(x => x.Stop());
            //        });
            //    });
            //}
            //catch (Exception e)
            //{
            //    LogHelper.GetLogHelper<ProxyService>().Error(e);
            //}
        }

        public void Stop()
        {

        }

        public int GetAllProxyCount()
        {
            return 0;
        }

        public List<ProxyEntity> GetProxyIps(int iCnt)
        {
            List<ProxyEntity> proxyIps = new List<ProxyEntity>();
            return proxyIps;
        }

        private static void Init()
        {
            //Log
            LogHelper.LogInit();

            // DI
            var builder = new ContainerBuilder();

            // TODO:Baibian Ip，Ip解码
            // builder.RegisterType<BaibianIpProxyProvider>().As<IProxyProvider>();
            builder.RegisterType<CoderBusyProxyProvider>().As<IProxyProvider>();
            builder.RegisterType<KuaidailiProxyProvider>().As<IProxyProvider>();
            builder.RegisterType<MayiProxyProvider>().As<IProxyProvider>();
            builder.RegisterType<SixIpProxyProvider>().As<IProxyProvider>();
            builder.RegisterType<XicidailiProxyProvider>().As<IProxyProvider>();
            builder.RegisterType<YundailiProxyProvider>().As<IProxyProvider>();
            var container = builder.Build();
            DependencyResolver.SetDependencyResolver(t => container.Resolve(t));

        }
    }
}
